//
//  TopApiLogin.m
//  mcf
//
//  Created by yuike6 yuike6 on 12-1-19.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "TopApiLogin.h"
#import "header.h"
//#import "ymall.h"

#define _APPSECRET_        @"91d3ed112d55ee22995d35cd0abd39da"


@implementation TopApiLogin
@synthesize key = key_;
@synthesize topsession = topsession_;
@synthesize sid = sid_;
@synthesize nick = nick_;
@synthesize token = token_;
@synthesize ecode = ecode_;

@synthesize loginUrl = loginUrl_;
@synthesize topKey;

@synthesize topToken = topToken_;
@synthesize appKey = appKey_;

@synthesize autoLogin = autoLogin_;

-(void) dealloc
{
    YRelease(key_);
    YRelease(topsession_);
    YRelease(sid_);
    YRelease(nick_);
    YRelease(token_);
    YRelease(ecode_);
    
    YRelease(loginUrl_);
    YRelease(topKey_);
    
    YRelease(topToken_);
    YRelease(appKey_);
    
    [super dealloc];
}


- (NSString *) time
{
    YRelease(time_);
    NSTimeInterval current = [[NSDate date] timeIntervalSince1970];
    time_ = [[NSString alloc] initWithFormat:@"%d", current];
    return time_;
}
- (NSString *) topToken
{
    YRelease(topToken_);
    NSString * tmp  = [NSString stringWithFormat:@"%@%@%@%@", 
                       [self.appKey gbkString], 
                       [[_APPSECRET_ gbkString] md5Digest],
                       [self.nick gbkString],
                       [self.time gbkString]];
    
    topToken_ = [[NSString alloc] initWithString:[tmp md5Digest]];
    
    return topToken_;
}

-(NSString *) topApiUrl : (NSString *) appkey callbackUrl:(NSURL *) callbackUrlValue
{
    if(appkey == nil ||
       [appkey length] <= 0)
    {
        return nil;
    }
    
    YkSystem * system = [[[YkSystem alloc] init] autorelease];   
    [self setTopApi:TOP_API_USER_LOGOIN];
    [self setTopApiV:nil];
    [self setTopApiT:nil];
    [self setTopApiIMEI:[system deviceImei]];
    [self setTopApiIMSI:[system deviceImsi]];
    [self setTopApiAppkey:appkey];
    
    NSDictionary *dictionary = nil;
    if(callbackUrlValue == nil)
    {
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:appkey, _PARAM_TOP_DATA_APPKEY_, nil];
    }
    else
    {
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:appkey, _PARAM_TOP_DATA_APPKEY_, callbackUrlValue, _PARAM_TOP_DATA_CALLBACK, nil];
    }
       
    CJSONSerializer * jsonSeroalizer = [[CJSONSerializer alloc] init];
    NSString *jsonString = nil;
    jsonString = [jsonSeroalizer serializeObject:dictionary];
    YRelease(jsonSeroalizer);
    
    [self setTopApiData:jsonString];    
    return [super topApiUrl];
}

- (NSString *) loginUrlByTopApiRecvData: (TopApiRecvData *)topApiRecvData
{
    
    return nil;
    
//    
//    if (!topApiRecvData) {
//        return nil;
//    }
//    
//    NSString * data_1 = [topApiRecvData stringRecvDataVlaueByKey:@"url"];
//    self.topKey = [topApiRecvData stringRecvDataVlaueByKey:@"key"] ;
//    
//    NSString* webStringURL = [data_1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    self.loginUrl = webStringURL;
//    
//    return webStringURL;
}


- (void) setAutoLogin:(BOOL)autoLogin
{
    autoLogin_ = autoLogin;
    
    if (!autoLogin) 
    {
        return;
    }
    
    [self setTopApi:TOP_API_USER_AUTO_LOGOIN];    
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.token, @"token", self.appKey, @"appKey", self.topToken, @"topToken", nil];
    
    id jsonString = [[CJSONSerializer serializer] serializeObject:dictionary];    
    [self setTopApiData:jsonString];
}

//- (BOOL) isPastDue
//{
//    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
//    if (now - sinceTime >= _PAST_DUE_) 
//    {
//        return TRUE;
//    }
//    
//    return FALSE;
//}


@end
